package com.macro.mall.service.impl;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.BmsMpSlideshowParam;
import com.macro.mall.mapper.BmsMpSlideshowMapper;
import com.macro.mall.model.*;
import com.macro.mall.service.BmsMpSlideshowService;
import com.macro.mall.service.UmsAdminService;
import com.macro.mall.utils.UserUtils;
import com.mysql.cj.AbstractQuery;
import org.apache.http.protocol.HTTP;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.security.AccessController;
import java.security.Principal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 商品属性管理Service实现类
 * Created by macro on 2018/4/26.
 */
@Service
public class BmsMpSlideshowServiceImpl implements BmsMpSlideshowService {
    @Autowired
    private BmsMpSlideshowMapper bmsMpSlideshowMapper;
    @Autowired
    private UserUtils userUtils;

    /**
     * 分页查询轮播图
     * @param pageSize
     * @param pageNum
     * @return
     */
    @Override
    public List<BmsMpSlideshow> getList(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        BmsMpSlideshowExample example = new BmsMpSlideshowExample();
        BmsMpSlideshowExample.Criteria criteria = example.createCriteria();
        example.setOrderByClause("sort desc, create_time desc");
        List<BmsMpSlideshow> list = bmsMpSlideshowMapper.selectByExample(example);
        return list;
    }

    /**
     * 新增轮播图
     * @param bmsMpSlideshowParam
     * @return
     */
    @Override
    public int create(BmsMpSlideshowParam bmsMpSlideshowParam) {
        //获取当前登录用户id
        Long userId = userUtils.getUserId();
        String string = UUID.randomUUID().toString();
        BmsMpSlideshow bmsMpSlideshow = new BmsMpSlideshow();
        BeanUtils.copyProperties(bmsMpSlideshowParam, bmsMpSlideshow);
        bmsMpSlideshow.setCreateTime(new DateTime());
        bmsMpSlideshow.setUpdateTime(new DateTime());
        bmsMpSlideshow.setId(string);
        bmsMpSlideshow.setCreateUser(userId);
        bmsMpSlideshow.setUpdateUser(userId);
        int count = bmsMpSlideshowMapper.insertSelective(bmsMpSlideshow);
        return count;
    }

    /**
     * 更新轮播图
     * @param bmsMpSlideshowParam
     * @return
     */
    @Override
    public int update(BmsMpSlideshowParam bmsMpSlideshowParam) {
        //获取当前登录用户id
        Long userId = userUtils.getUserId();
        BmsMpSlideshow bmsMpSlideshow = new BmsMpSlideshow();
        bmsMpSlideshow.setId(bmsMpSlideshowParam.getId());
        BeanUtils.copyProperties(bmsMpSlideshowParam, bmsMpSlideshow);
        bmsMpSlideshow.setUpdateTime(new DateTime());
        bmsMpSlideshow.setUpdateUser(userId);
        return bmsMpSlideshowMapper.updateByPrimaryKeySelective(bmsMpSlideshow);
    }

    /**
     * 获取单个轮播图信息
     * @param id
     * @return
     */
    @Override
    public BmsMpSlideshow getItem(Long id) {
        return bmsMpSlideshowMapper.selectByPrimaryKey(id);
    }

    /**
     * 删除轮播图
     * @param ids
     * @return
     */
    @Override
    public int delete(List<Long> ids) {
        BmsMpSlideshowExample example = new BmsMpSlideshowExample();
        example.createCriteria().andIdIn(ids);
        Long userId = userUtils.getUserId();
        example.createCriteria().andUpdateUserEqualTo(userId);
        example.createCriteria().andUpdateTimeEqualTo(new DateTime());
        example.createCriteria().andIsDelete(false);
        int count = bmsMpSlideshowMapper.deleteByExample(example);
        return count;
    }

    @Override
    public int updateShowStatus(BmsMpSlideshowParam bmsMpSlideshowParam) {
        //获取当前登录用户id
        Long userId = userUtils.getUserId();
        BmsMpSlideshow bmsMpSlideshow = new BmsMpSlideshow();
        BeanUtils.copyProperties(bmsMpSlideshowParam, bmsMpSlideshow);
        bmsMpSlideshow.setId(bmsMpSlideshowParam.getId());
        bmsMpSlideshow.setIsEnabled(bmsMpSlideshowParam.getIsEnabled());
        bmsMpSlideshow.setUpdateTime(new DateTime());
        bmsMpSlideshow.setUpdateUser(userId);
        return bmsMpSlideshowMapper.updateByPrimaryKeySelective(bmsMpSlideshow);
    }



    /**
     * 小程序查询轮播图
     * @return
     */
    @Override
    public List<BmsMpSlideshow> getMpList() {
        BmsMpSlideshowExample example = new BmsMpSlideshowExample();
        BmsMpSlideshowExample.Criteria criteria = example.createCriteria();
        criteria.andIsDelete(true).andIsEnabled(1);
        example.setOrderByClause("sort desc, create_time desc");
        return bmsMpSlideshowMapper.selectByExample(example);
    }
}
